/*
* Copyright (C) Microsoft. All rights reserved.  
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not 
* use this file except in compliance with the License.  You may obtain a copy 
* of the License at http://www.apache.org/licenses/LICENSE-2.0  
* 
* THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED 
* WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, 
* MERCHANTABLITY OR NON-INFRINGEMENT. 
*
* See the Apache License, Version 2.0 for specific language governing 
* permissions and limitations under the License.
*
*/
#pragma once

#include <baja/common.hpp>

#if !defined(BAJA_WIN32)
#error "stopwatch only supported for win32"
#else

#include <windows.h>

namespace baja
{
class stopwatch
{
private:
    LARGE_INTEGER   _startTime;
    double          _toSeconds;

public:
    stopwatch() : _toSeconds(0.f)
    {
        LARGE_INTEGER freq;
        if (QueryPerformanceFrequency(&freq))
        {
            _toSeconds = 1.0 / (double)freq.QuadPart;
        }

        // reset the stopwatch (which starts it counting)
        this->reset();
    }

    void reset()
    {
        _startTime.QuadPart = 0;
        QueryPerformanceCounter(&_startTime);
    }

    double getElapsedSeconds()
    {
        LARGE_INTEGER current;

        if (QueryPerformanceCounter(&current))
        {
            return (current.QuadPart - _startTime.QuadPart) * _toSeconds;
        }

        return 0.0;
    }

    double getElapsedMilliseconds()
    {
        LARGE_INTEGER current;

        if (QueryPerformanceCounter(&current))
        {
            return (current.QuadPart - _startTime.QuadPart) * _toSeconds * 1000.0;
        }

        return 0.0;
    }
};
#endif

} // namespace baja
